package com.fbt.transfer.app.driver.ui.index;

import android.content.Context;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.fbt.transfer.app.driver.model.PageInfo;
import com.fbt.transfer.app.driver.model.Result;
import com.fbt.transfer.app.driver.model.index.IndexStats;
import com.fbt.transfer.app.driver.util.AppContextProvider;
import com.fbt.transfer.app.driver.http.AbstractCallback;
import com.fbt.transfer.app.driver.http.ApiClient;
import com.fbt.transfer.app.driver.http.ApiInterface;
import com.fbt.transfer.app.driver.model.index.OrderUnitVo;

import java.util.ArrayList;
import java.util.List;
import lombok.Getter;

public class IndexViewModel extends ViewModel {

  @Getter
  private final MutableLiveData<List<OrderUnitVo>> todayOrdersLiveData = new MutableLiveData<>();
  @Getter
  private final MutableLiveData<List<OrderUnitVo>> tomorrowOrdersLiveData = new MutableLiveData<>();
  @Getter
  private final MutableLiveData<Boolean> loadingLiveData = new MutableLiveData<>();
  @Getter
  private final MutableLiveData<String> errorLiveData = new MutableLiveData<>();
  @Getter
  private final MutableLiveData<Boolean> emptyTodayDataLiveData = new MutableLiveData<>();
  @Getter
  private final MutableLiveData<Boolean> emptyTomorrowDataLiveData = new MutableLiveData<>();
  @Getter
  private final MutableLiveData<Integer> todayOrderCountLiveData = new MutableLiveData<>(0);
  @Getter
  private final MutableLiveData<Integer> tomorrowOrderCountLiveData = new MutableLiveData<>(0);
  @Getter
  private final MutableLiveData<IndexStats> indexStatsLiveData = new MutableLiveData<>();

  private final ApiInterface apiInterface;
  private int currentTodayPage = 1;
  private int currentTomorrowPage = 1;
  @Getter
  private boolean isLastTodayPage = false;
  @Getter
  private boolean isLastTomorrowPage = false;
  private final Context context;

  public IndexViewModel () {
    apiInterface = ApiClient.getApiInterface();
    context = AppContextProvider.getAppContext();
    todayOrdersLiveData.setValue(new ArrayList<>());
    tomorrowOrdersLiveData.setValue(new ArrayList<>());
    loadIndexStats();
  }
  
  /**
   * 加载首页统计信息
   */
  public void loadIndexStats() {
    apiInterface.indexStats()
        .enqueue(new AbstractCallback<IndexStats>(context, true) {
          @Override
          public void onSuccess(IndexStats stats) {
            indexStatsLiveData.setValue(stats);
          }
          
          @Override
          protected void onFailure(Result<IndexStats> result) {
            errorLiveData.setValue(result.getMessage());
          }
        });
  }

  public void loadTodayOrders (boolean isRefresh) {
    if (isRefresh) {
      currentTodayPage = 1;
      isLastTodayPage = false;
    }
    loadingLiveData.setValue(true);

    apiInterface.indexOrderList(currentTodayPage, 10, true)
        .enqueue(new AbstractCallback<PageInfo<OrderUnitVo>>(context, true) {

          @Override
          public void onSuccess (PageInfo<OrderUnitVo> pageInfo) {
            loadingLiveData.setValue(false);
            List<OrderUnitVo> orders = pageInfo.getRecords();

            if (isRefresh) {
              todayOrdersLiveData.setValue(orders);
            } else if (orders != null && !orders.isEmpty()) {
              List<OrderUnitVo> currentList = todayOrdersLiveData.getValue();
              if (currentList != null) {
                currentList.addAll(orders);
                todayOrdersLiveData.setValue(currentList);
              }
            }

            isLastTodayPage = !pageInfo.hasNext();
            emptyTodayDataLiveData.setValue(orders == null || orders.isEmpty());
            todayOrderCountLiveData.setValue(pageInfo.getTotal());
          }

          @Override
          protected void onFailure (Result<PageInfo<OrderUnitVo>> result) {
            super.onFailure(result);
            loadingLiveData.setValue(false);
            errorLiveData.setValue(result.getMessage());
          }

        });
  }

  public void loadTomorrowOrders (boolean isRefresh) {
    if (isRefresh) {
      currentTomorrowPage = 1;
      isLastTomorrowPage = false;
    }
    loadingLiveData.setValue(true);

    apiInterface.indexOrderList(currentTomorrowPage, 10, false)
        .enqueue(new AbstractCallback<PageInfo<OrderUnitVo>>(context, true) {
          @Override
          public void onSuccess (PageInfo<OrderUnitVo> pageInfo) {
            loadingLiveData.setValue(false);
            List<OrderUnitVo> orders = pageInfo.getRecords();

            if (isRefresh) {
              tomorrowOrdersLiveData.setValue(orders);
            } else if (orders != null && !orders.isEmpty()) {
              List<OrderUnitVo> currentList = tomorrowOrdersLiveData.getValue();
              if (currentList != null) {
                currentList.addAll(orders);
                tomorrowOrdersLiveData.setValue(currentList);
              }
            }

            isLastTomorrowPage = !pageInfo.hasNext();
            emptyTomorrowDataLiveData.setValue(orders == null || orders.isEmpty());
            tomorrowOrderCountLiveData.setValue(pageInfo.getTotal());
          }

          @Override
          protected void onFailure (Result<PageInfo<OrderUnitVo>> result) {
            loadingLiveData.setValue(false);
            errorLiveData.setValue(result.getMessage());
          }
        });
  }

  public void loadNextTodayPage () {
    if (!isLastTodayPage) {
      currentTodayPage++;
      loadTodayOrders(false);
    }
  }

  public void loadNextTomorrowPage () {
    if (!isLastTomorrowPage) {
      currentTomorrowPage++;
      loadTomorrowOrders(false);
    }
  }

}